#if CONFIG_SDR_TESTER
#include "Sdr.h"
#include "Command_Define.h"
#include "SdrTester.h"
#include "Debug.h"

#define MAX_SDR_COUNT 10


static Sdr g_SdrTester;
static SdrHeader* pSdrHeader[MAX_SDR_COUNT];
static int g_Tester_SdrCount = 0;
static int g_Tester_SensorCount = 0;
static int g_Tester_ipmbAddr = 0xC2;
static int g_Tester_siteNum = 1;
Sdr* SdrTester_Create()
{
	
	Trace("Create Sdr: max sdr count is [%d]\n", MAX_SDR_COUNT);
	memset(pSdrHeader, 0, sizeof(pSdrHeader));
	g_Tester_SdrCount = 0;
	g_Tester_SensorCount = 0;
	Sdr_Init(&g_SdrTester, pSdrHeader, MAX_SDR_COUNT);

	SdrTester_CreateRepository(g_Tester_ipmbAddr, g_Tester_siteNum);
	return &g_SdrTester;
}

void SdrTester_Release()
{
	Trace("Release Sdr\n");
	g_Tester_SdrCount = 0;
	g_Tester_SensorCount = 0;
	memset(pSdrHeader, 0, sizeof(pSdrHeader));
	Sdr_Init(&g_SdrTester, pSdrHeader, MAX_SDR_COUNT);
}


static DevLocatorRecord 	g_DevicLocatorRecord;	
static CompactSensorRecord	g_HandleCompactSensorRecord;
static FullSensorRecord		g_Temp1FullSensorRecord;
static FullSensorRecord		g_Temp2FullSensorRecord;

Threshold*	g_pSdrTesterThresholdItem = Null;

Bool SdrTester_CreateRepository( uint8 ipmbAddr, uint8 siteNum)
{
	Sdr* pSdr = &g_SdrTester;
	int i = 0;
	int sensorId = 0;
	
	
	static SensorItem g_SensorItem[] =
	{ 
		 {ET_SPECIFIC , 1	, MODULE_HOT_SWAP		, 0x00, 0x1F, 0x00, 0x1C, 0xC0, 0, 0, "Handle"}
		,{ET_THRESHOLD, 2	, TEMPERATURE_SENSOR	, 0x7A, 0x95, 0x7A, 0x95, FULL_SENSOR_RECORD	, 0, 0, "Temp1"}
		,{ET_THRESHOLD, 3	, TEMPERATURE_SENSOR	, 0x0A, 0x80, 0x0A, 0x80, FULL_SENSOR_RECORD	, 0, 0, "Temp2"}
	};
	static Threshold g_ThresholdItem[] =
	{
		  {80, 70, 60, 50, 40, 30, 2, 2}	// for Temp Sensor
		, {80, 70, 60, 0, 0, 0, 2, 2}		// for Temp Sensor
		, {100, 90, 80, 70, 60, 50, 2, 2}	// for Input-V Sensor
	};

	g_pSdrTesterThresholdItem = &g_ThresholdItem[0];
	
	Trace("Create Sdr repository\n");
	g_Tester_SdrCount = 0;
	g_Tester_SensorCount = 0;
	
	DevLocatorRecord_Init(&g_DevicLocatorRecord, g_Tester_SdrCount++, ipmbAddr, siteNum, "Mcmc");
	Sdr_Add(pSdr, (SdrHeader*)&g_DevicLocatorRecord);
	//Trace("pSdr->m_pRecordArray[%d].id=%d\n", recordId-1, pSdr->m_pRecordArray[recordId-1]->m_Id);
	
	CompactSensorRecord_Init(&g_HandleCompactSensorRecord, g_Tester_SdrCount++, &g_SensorItem[sensorId++], ipmbAddr, siteNum);
	Sdr_Add(pSdr, (SdrHeader*)&g_HandleCompactSensorRecord);
	g_Tester_SensorCount++;
	//Trace("pSdr->m_pRecordArray[%d].id=%d\n", recordId-1, pSdr->m_pRecordArray[recordId-1]->m_Id);
	
	FullSensorRecord_Init(&g_Temp1FullSensorRecord,g_Tester_SdrCount++, &g_SensorItem[sensorId++], &g_ThresholdItem[0], ipmbAddr, siteNum);
	Sdr_Add(pSdr, (SdrHeader*)&g_Temp1FullSensorRecord);
	g_Tester_SensorCount++;
	
	FullSensorRecord_Init(&g_Temp2FullSensorRecord,g_Tester_SdrCount++, &g_SensorItem[sensorId++], &g_ThresholdItem[1], ipmbAddr, siteNum);
	Sdr_Add(pSdr, (SdrHeader*)&g_Temp2FullSensorRecord);
	g_Tester_SensorCount++;
	

	i = 0;
	if(pSdrHeader[i++] != (SdrHeader*)&g_DevicLocatorRecord	) return False;
	if(pSdrHeader[i++] != (SdrHeader*)&g_HandleCompactSensorRecord) return False;
	if(pSdrHeader[i++] != (SdrHeader*)&g_Temp1FullSensorRecord) return False;
	if(pSdrHeader[i++] != (SdrHeader*)&g_Temp2FullSensorRecord) return False;

	return True;
}

Bool SdrTester_GetSdrCount()
{
	int nCount = 0;
	
	SdrTester_Create();
	nCount = Sdr_GetSdrCount(&g_SdrTester, False);
	
	if(g_Tester_SdrCount == nCount)
	{
		TRACE_LEVEL(TRACE_SDR, ("Get Sdr Count = %d, Test Pass\n", nCount));
	}
	else
	{
		TRACE_LEVEL(TRACE_SDR, ("Get Sdr Count = %d[%d], Test Failed\n", nCount, g_Tester_SdrCount));
		return False;
	}

	nCount = Sdr_GetSdrCount(&g_SdrTester, True);
	if(g_Tester_SensorCount == nCount)
	{
		TRACE_LEVEL(TRACE_SDR, ("Get Sensor Count = %d, Test Pass\n", nCount));
	}
	else
	{
		TRACE_LEVEL(TRACE_SDR, ("Get Sensor Count = %d[%d], Test Failed\n", nCount, g_Tester_SensorCount));
		return False;
	}

	return True;
}

Bool SdrTester_Sdr_GetRecord()
{
	int i = 0;
	uint8 buff[0xFF];
	uint8 nReadLen = 0xFF;
	uint8 offset = 0;
	uint16 recordId = 0;
	uint16 nextRecordId = 0;
	uint8 len = 0xFF;
	uint8* pData = Null;

	TRACE_LEVEL(TRACE_SDR, ("Test Function: uint8 Sdr_GetRecord(Sdr* pSdr, uint16 recordId, uint16* pNextRecordId, uint8 offset, uint8* pBuff, uint8 len)\n"));

	SdrTester_Create();

	nReadLen = Sdr_GetRecord(&g_SdrTester, recordId, &nextRecordId, offset, buff, len);
	
	TRACE_LEVEL(TRACE_SDR, ("Sdr_GetRecord(0x%08x, %d, %d, %d, 0x%08x, %d) = %d\n"
		,&g_SdrTester, recordId, nextRecordId, offset, buff, len, nReadLen ));
	
	if(nReadLen)
	{
		if(nReadLen != g_DevicLocatorRecord.m_SdrHeader.m_Len + SDR_HEADER_LEN - offset)
		{
			return False;
		}	
		if(memcmp(&buff, &g_DevicLocatorRecord, sizeof(g_DevicLocatorRecord) != 0))
		{
			return False;
		}
		if(nextRecordId != g_HandleCompactSensorRecord.m_SdrHeader.m_Id)
		{
			return False;
		}
	}
	else
	{
		return False;
	}

	offset = 10;
	len = 12;
	recordId = g_HandleCompactSensorRecord.m_SdrHeader.m_Id;
	nReadLen = Sdr_GetRecord(&g_SdrTester, recordId, &nextRecordId, offset, buff, len);
	TRACE_LEVEL(TRACE_SDR, ("Sdr_GetRecord(0x%08x, %d, %d, %d, 0x%08x, %d) = %d\n"
		,&g_SdrTester, recordId, nextRecordId, offset, buff, len, nReadLen ));
	if(nReadLen)
	{
		if(nReadLen != len)
		{
			return False;
		}	
		pData = (uint8*)&g_HandleCompactSensorRecord;

		if(memcmp(buff, &pData[offset], nReadLen) != 0)
		{
			return False;
		}
		if(nextRecordId != g_Temp1FullSensorRecord.m_SdrHeader.m_Id)
		{
			return False;
		}
	}
	else
	{
		return False;
	}
	
	offset = 10;
	len = 0xF0;
	recordId = g_Temp1FullSensorRecord.m_SdrHeader.m_Id;
	nReadLen = Sdr_GetRecord(&g_SdrTester, recordId, &nextRecordId, offset, buff, len);
	TRACE_LEVEL(TRACE_SDR, ("Sdr_GetRecord(0x%08x, %d, %d, %d, 0x%08x, %d) = %d\n"
		,&g_SdrTester, recordId, nextRecordId, offset, buff, len, nReadLen ));
	if(nReadLen)
	{
		if(nReadLen != g_Temp1FullSensorRecord.m_SdrHeader.m_Len + SDR_HEADER_LEN - offset)
		{
			return False;
		}	
		pData = (uint8*)&g_Temp1FullSensorRecord;
		if(memcmp(buff, &pData[offset], nReadLen) != 0)
		{
			return False;
		}
		if(nextRecordId != g_Temp2FullSensorRecord.m_SdrHeader.m_Id)
		{
			return False;
		}
	}
	else
	{
		return False;
	}

	offset = 12;
	len = 0xF0;
	recordId = g_Temp2FullSensorRecord.m_SdrHeader.m_Id;
	nReadLen = Sdr_GetRecord(&g_SdrTester, recordId, &nextRecordId, offset, buff, len);
	TRACE_LEVEL(TRACE_SDR, ("Sdr_GetRecord(0x%08x, %d, %d, %d, 0x%08x, %d) = %d\n"
		,&g_SdrTester, recordId, nextRecordId, offset, buff, len, nReadLen ));
	if(nReadLen)
	{
		if(nReadLen != g_Temp2FullSensorRecord.m_SdrHeader.m_Len + SDR_HEADER_LEN - offset)
		{
			return False;
		}	
		pData = (uint8*)&g_Temp2FullSensorRecord;
		if(memcmp(buff, &pData[offset], nReadLen) != 0)
		{
			return False;
		}
		if(nextRecordId != 0)
		{
			return False;
		}
	}
	else
	{
		return False;
	}
	
	return True;
}

Bool SdrTester_GetRepositoryInfo()
{
	uint8 buff[14];
	uint16 recordCount = 0;
	
	TRACE_LEVEL(TRACE_SDR, ("Test Function: void Sdr_GetRepositoryInfo(Sdr* pSdr, uint8* pBuff, uint8 len)\n"));
	SdrTester_Create();

	Sdr_GetRepositoryInfo(&g_SdrTester, buff, 14);

	recordCount = (uint16)((buff[2]<<8) | buff[1]);

	TRACE_LEVEL(TRACE_SDR, ("SDR_VERSION = %x\n", buff[1]));
	if(buff[0] != SDR_VERSION) return False;
	TRACE_LEVEL(TRACE_SDR, ("recordCount = %x\n", recordCount));
	if(recordCount != g_Tester_SdrCount) return False;

	return True;
}
#endif
